/**
 * 用户管理JavaScript
 *
 * Created by Zmt on 2015/6/19.
 */
var storeURL;

var resetUrl;

$().ready(function ()
{
    $('#dg').datagrid({checkOnSelect: $(this).is(':checked')})
});

// 搜索
function search()
{
    var roleId;
    var role_input = $('#roleId');
    var text = role_input.combobox("getText");
    if (null != text && "" != text)
    {
        roleId = role_input.combobox("getValue");
        if (null == roleId || "" == roleId)
            $('#dg').datagrid('reload');
    }
    $('#dg').datagrid('load', {account: $('#account').val(), name: $('#name').val(), role_id: roleId});
}

// 跳到新建用户页面
function create()
{
    $('#pwd').validatebox({
        required: true
    });
    $('#rePwd').validatebox({
        required: true
    });
    $('#dlg').dialog('open').dialog('setTitle', '添加用户');
    $('#fm').form('clear');
    $("#js_pwd, #js_repwd").show();
    $('#insert_type').combobox('setValue',0);
    isModify(0);
    $('js_account').disabled = false;
    $('#dlg-buttons').show();
    storeURL = 'create.do';
}

//跳到修改页面
function modify(index)
{
    $('#dg').datagrid('selectRow', index);
    var row = $('#dg').datagrid('getSelected');
    if (row)
    {
        $('#pwd,#rePwd').validatebox({required: false});
        $('#dlg').dialog('open').dialog('setTitle', '修改用户');
        $('#fm').form('clear');
        $('#fm').form('load', row);
        isModify(1);
        document.getElementById('js_account').disabled = true;
        $("#js_pwd, #js_repwd").hide();
        storeURL = "modify.do?id=" + row.id;
    }
}

//保存
function store()
{
    $('#fm').form('submit', {
        url: storeURL,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result)
        {
            var result = $.parseJSON(result);
            if (result.ok)
            {
                $('#dlg').dialog('close');
                $('#dg').datagrid('reload');
                $.messager.show({
                    title: '提示信息',
                    msg: '操作成功!'
                });
            }
            else
            {
                $.messager.alert("提示信息", result.msg);
            }
        }
    });
}

function storePwd()
{
    resetUrl = resetUrl + "&&pass=" + $('#re_pwd').val();
    var pwd=$('#re_pwd').val().toString();
    var re_re_pwd=$('#re_rePwd').val().toString();
    if(pwd==null||pwd.length==0||re_re_pwd==null||re_re_pwd.length==0||pwd!=re_re_pwd){
       alert("密码错误，请重新输入！");
        return ;
    }else{
        $.ajax({
            type: 'POST',
            url: resetUrl,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success:function(obj)
            {
                obj = jQuery.parseJSON(obj);
                if (obj.ok)
                {
                    $('#dlg2').dialog('close');
                    $('#dg').datagrid('reload');
                    $.messager.show({
                        title: '提示信息',
                        msg: '重置密码成功!'
                    });
                    if (obj.data)
                        $.location.reload();
                }
                else
                    $.messager.alert("提示信息", result.msg);
            }
        });
    }

}

function safeDelete(url)
{
    var checkedItems = $('#dg').datagrid('getChecked');
    if (checkedItems != "") {
        var ids = [];
        $.each(checkedItems, function (index, item)
        {
            if (item.id != "")
                ids.push(item.id);
        });
        if (ids.length != 0) {
            $.messager.confirm('删除确认', '删除后将无法恢复，您确定要删除吗？', function (r) {
                if (r) {
                    $.post(url + '/safeDelete.do?ids=' + ids, function (result) {
                        if (result.ok) {
                            $.messager.show({
                                title: '成功',
                                msg: "删除成功"
                            });
                            $('#dg').datagrid('reload');
                        }
                        else {
                            alert(result.msg);
                        }
                    }, 'json');
                }
            });
        } else {
            $.messager.show({title: '失败', msg: "需选中删除项"});
        }
    }else{
        $.messager.alert("提示信息", "未选中任何用户");
    }
}

//密码框验证
function shiftValid()
{
    if ($("#").val() != '')
        $('#rePwd').validatebox({required: true});
    else
        $('#rePwd').validatebox({required: false});
}

function choseCol()
{
    $("#js_sellect_col").dialog("open").dialog("setTitle", "选择栏目");
}

function setCol()
{
    var row = $('#col_dg').datagrid('getSelected');
    if (row)
    {
        $('#js_col_name').val(row.title);
        $('#js_col').val(row.id);
    }
    $("#js_sellect_col").dialog('close');
}

function isModify(id)
{
    if (id == 1)
    {
        $('#js_pwd').hide();
        $('#js_repwd').hide();
    }
    else
    {
        $('#js_pwd').show();
        $('#js_repwd').show();
    }
}
function isOperate(value, row, index)
{
    var item = "<button href=\"javascript:void(0)\" class=\"easyui-linkbutton\" iconCls=\"icon-ok\" plain=\"true\"  onclick=\"modify('"+index+"')\">修改</button>&nbsp;"+
        "<button href=\"javascript:void(0)\" class=\"easyui-linkbutton\" iconCls=\"icon-ok\" plain=\"true\"  onclick=\"resetPwd('"+index+"')\">重置密码</button>&nbsp;";
    return item;
}

function resetPwd(index)
{
    $('#dg').datagrid('selectRow', index);
    var row = $('#dg').datagrid('getSelected');
    if(row)
    {
        $('#dlg2').dialog('open').dialog('setTitle', '重置密码');
        $('#re_pwd').val("");
    }
    resetUrl = "/user/resetPass.do?id=" + row.id;
}

//查看详情
function getView()
{
    var row = $('#dg').datagrid('getSelected');
    if (row)
    {
        $('#dlg').dialog('open').dialog('setTitle', '用户详情');
        $("#add").hide();
        $('#fm').form('load', row);
        $('input').attr("disabled", "disabled");  // 将input元素设置为disabled
        $("#js_pwd, #js_repwd").hide();
    }
}

// 取消
function cancel()
{
    $('#dlg').dialog('close');
    $('input').removeAttr("disabled");  //去除input元素的只读属性

}

/**
 * “选择栏目”搜索
 */
function searchCol()
{
    $('#col_dg').datagrid('load', {title: $('#js_title').val(), recommend: $('#js_rec').combobox('getValue')});
}